package com.asia.order.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 功能:本类功能为将各种对象转化为Json字符串
 * 
 */
public class JsonToolUtil {
	public static Logger log = LoggerFactory.getLogger(JsonToolUtil.class);
	
	/**
	 * 将 List 转化为Json字符串
	 * @param List dataList （可存map或databean或者混合存储都可以）
	 * @return String JsonString
	 */
	public static String listToJsonString(List dataList){
		ObjectMapper mapper = new ObjectMapper(); 
		String jsonString = "";
		try {
			jsonString = mapper.writeValueAsString(dataList);
		} catch(Exception e){
			if(log.isDebugEnabled()){
				log.debug(" JSONObject Exception="+e.getMessage());
		    }
		}
		return jsonString;
	}
	/**
	 * 将 Map 转化为Json字符串
	 * @param Map map
	 * @return String JsonString
	 */
	public static String mapToJsonString(Map map){
		ObjectMapper mapper = new ObjectMapper(); 
		String jsonString = "";
		try {
			jsonString = mapper.writeValueAsString(map);
		} catch(Exception e){
			if(log.isDebugEnabled()){
				log.debug(" JSONObject Exception="+e.getMessage());
		    }
		}
		return jsonString;
	}
	/**
	 * 将 JavaBean 类型的对象   转化为Json字符串
	 * @param Object obj  
	 * @return String JsonString
	 */
	public static String beanToJsonString(Object obj){
		ObjectMapper mapper = new ObjectMapper(); 
		String jsonString = "";
		try {
			jsonString = mapper.writeValueAsString(obj);
		} catch(Exception e){
			if(log.isDebugEnabled()){
				log.debug(" JSONObject Exception="+e.getMessage());
		    }
		}
		return jsonString;
	}
	/**
	 * 将 Object 转化为Json字符串
	 * @param Object obj  
	 * @return String JsonString
	 */
	public static String objectToJsonString(Object obj){
		ObjectMapper mapper = new ObjectMapper(); 
		String jsonString = "";
		try {
			jsonString = mapper.writeValueAsString(obj);
		} catch(Exception e){
			if(log.isDebugEnabled()){
				log.debug(" JSONObject Exception="+e.getMessage());
		    }
		}
		return jsonString;
	}
       
	/**
	 * 将 Json String 转化为Map
	 * @param String JsonString  
	 * @return Map returnMap
	 */
	public static Map getMapFromJsonString(String JsonString) {
		Map returnMap = new HashMap();
		ObjectMapper mapper = new ObjectMapper(); 
		try{
			returnMap = mapper.readValue(JsonString, Map.class);
		}catch(Exception e){
			if(log.isDebugEnabled()){
				log.debug(" JSONObject Exception="+e.getMessage());
		    }
		}
		return returnMap;
	}
	/**
	 * 将 Json String 转化为List(Json中List对象名称为tag)
	 * @param String JsonString  
	 * @return Map returnList
	 */
	public static List getListFromJsonString(String JsonString,String tag) {
		List returnList = new ArrayList();
		ObjectMapper mapper = new ObjectMapper(); 
		try{
			Map returnMap = mapper.readValue(JsonString, Map.class);
			if(returnMap.containsKey(tag)){
				returnList = (List)returnMap.get(tag);
			}
		}catch(Exception e){
			if(log.isDebugEnabled()){
				log.debug(" JSONObject Exception="+e.getMessage());
		    }
		}
		
		return returnList;
	}
	
	/**
	 * 将 Json String 转化为List(Json为数组)
	 * @param String JsonString  
	 * @return Map returnList
	 */
	public static List getListFromJsonString(String JsonString) {
		List returnList = new ArrayList();
		ObjectMapper mapper = new ObjectMapper(); 
		try{
			returnList = mapper.readValue(JsonString, List.class);
		}catch(Exception e){
			if(log.isDebugEnabled()){
				log.debug(" JSONObject Exception="+e.getMessage());
		    }
		}
		
		return returnList;
	}
	public static void main(String[] args) {
		String jsonStr = "{\"password\":\"123456\",\"username\":\"张三\"}";
		Map map = getMapFromJsonString(jsonStr);
		System.out.print("map="+map);
	}
}